package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.sdk.app.PayTask;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.demo.PayResult;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.Map;

public class MineAskNumberActivty extends BaseActivity implements View.OnClickListener{

    private Button youmin;
    private Button xiaogong;
    private Button pinnong;
    private Button funong;
    private Button duzhu;
    private Button zhixian;
    private Button zhifu;
    private Button zongdu;
    private Button wangye;
    private Button diwang;
    //等级标志
    private int one = 0,two = 100,three = 200,four = 500,five = 1000,six = 3000,seven = 5000,eight = 10000,nine = 30000,ten = 50000;
    // 当前级别
    private int money = 0;
    private int count = 0;
    private String a ="0";
    private String i;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private static final int SDK_PAY_FLAG = 1;
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PayResult payresult = new PayResult((Map<String, String>)msg.obj);
            String resultStatus = payresult.getResultStatus();
            if (resultStatus.equals("9000")){
                Toast.makeText(MineAskNumberActivty.this, "支付成功，您可以接单了", Toast.LENGTH_SHORT).show();
            }else {
            }
        }
    };
    private String data;
    private Button askUpData;
    private ImageView askback;
    private TextView textno;
    private TextView said;
    private TextView yivix;
    private String gradeCode;
    private TextView grade;
    private int mmm;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_ask_number_activty);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        Intent intent = getIntent();
        gradeCode = intent.getStringExtra("gradeCode");

        initAskView();
        int money = getMoney(gradeCode);
        textno.setText(money+"");
        switch (Integer.valueOf(gradeCode)){
            case 0:
                grade.setText("居民");
                youmin.setSelected(true);
                break;
            case 1:
                grade.setText("居民");
                youmin.setSelected(true);
                break;
            case 2:
                grade.setText("小工");
                xiaogong.setSelected(true);
                break;
            case 3:
                grade.setText("贫农");
                pinnong.setSelected(true);
                break;
            case 4:
                grade.setText("富农");
                funong.setSelected(true);
                break;
            case 5:
                grade.setText("地主");
                duzhu.setSelected(true);
                break;
            case 6:
                grade.setText("知县");
                zhixian.setSelected(true);
                break;
            case 7:
                grade.setText("知府");
                zhifu.setSelected(true);
                break;
            case 8:
                grade.setText("总督");
                zongdu.setSelected(true);
                break;
            case 9:
                grade.setText("王爷");
                wangye.setSelected(true);
                break;
            case 10:
                grade.setText("帝王");
                diwang.setSelected(true);
                break;
        }

    }

    private void initAskView() {
        youmin = (Button) findViewById(R.id.mine_member_bt1);
        xiaogong = (Button) findViewById(R.id.mine_member_bt2);
        pinnong = (Button) findViewById(R.id.mine_member_bt3);
        funong = (Button) findViewById(R.id.mine_member_bt4);
        duzhu = (Button) findViewById(R.id.mine_member_bt5);
        zhixian = (Button) findViewById(R.id.mine_member_bt6);
        zhifu = (Button) findViewById(R.id.mine_member_bt7);
        zongdu = (Button) findViewById(R.id.mine_member_bt8);
        wangye = (Button) findViewById(R.id.mine_member_bt9);
        diwang = (Button) findViewById(R.id.mine_member_bt10);
        askUpData = (Button) findViewById(R.id.mine_askupdata);
        askback = (ImageView) findViewById(R.id.mine_askmember_back);
        textno = (TextView) findViewById(R.id.ask_textno);
        said = (TextView) findViewById(R.id.ask_said);
        yivix = (TextView) findViewById(R.id.yivi_x);
        grade = (TextView) findViewById(R.id.ask_grade);
        backmain = (ImageView) findViewById(R.id.asknumber_backmain);

        // 设置监听事件
        backmain.setOnClickListener(this);
        yivix.setOnClickListener(this);
        said.setOnClickListener(this);
        youmin.setOnClickListener(this);
        xiaogong.setOnClickListener(this);
        pinnong.setOnClickListener(this);
        funong.setOnClickListener(this);
        duzhu.setOnClickListener(this);
        zhixian.setOnClickListener(this);
        zhifu.setOnClickListener(this);
        zongdu.setOnClickListener(this);
        wangye.setOnClickListener(this);
        diwang.setOnClickListener(this);
        askUpData.setOnClickListener(this);
        askback.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.asknumber_backmain:
                startActivity(new Intent(MineAskNumberActivty.this, MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
            case R.id.mine_askmember_back:
                finish();
                break;
            case R.id.yivi_x:
                Intent in = new Intent(MineAskNumberActivty.this,WEBXActivity.class);
                in.putExtra("from","yivi");
                in.putExtra("url", InternetAddressUtis.XYIVIREGISTER);
                startActivity(in);
                break;
            case R.id.ask_said:
                Intent in1 = new Intent(MineAskNumberActivty.this,WEBXActivity.class);
                in1.putExtra("from","yivier");
                in1.putExtra("url", InternetAddressUtis.XYIVIERGRADE);
                startActivity(in1);
                break;
            case R.id.mine_member_bt1:
                a = "1";
                youmin.setSelected(true);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                count=one;
                break;
            case R.id.mine_member_bt2:
                a = "2";

                youmin.setSelected(false);
                xiaogong.setSelected(true);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<two){
                    i = String.valueOf(two - count);
                }else{
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mine_member_bt3:

                a = "3";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(true);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<three){
                    i = String.valueOf(three - count);
                    Toast.makeText(this, i, Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mine_member_bt4:
                a = "4";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(true);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<four){
                    i = String.valueOf(four - count);

                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mine_member_bt5:

                a = "5";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(true);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<five){
                    i = String.valueOf(five - count);
                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mine_member_bt6:

                a = "6";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(true);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<six){
                    i = String.valueOf(six - count);

                }else{
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }


                break;
            case R.id.mine_member_bt7:

                a = "7";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(true);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<seven){
                    i = String.valueOf(seven - count);

                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.mine_member_bt8:

                a = "8";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(true);
                wangye.setSelected(false);
                diwang.setSelected(false);
                if (count<eight){
                    i = String.valueOf(eight - count);

                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.mine_member_bt9:

                a = "9";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(true);
                diwang.setSelected(false);
                if (count<nine){
                    i = String.valueOf(nine - count);

                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.mine_member_bt10:

                a = "10";
                youmin.setSelected(false);
                xiaogong.setSelected(false);
                pinnong.setSelected(false);
                funong.setSelected(false);
                duzhu.setSelected(false);
                zhixian.setSelected(false);
                zhifu.setSelected(false);
                zongdu.setSelected(false);
                wangye.setSelected(false);
                diwang.setSelected(true);
                if (count<ten){
                    i = String.valueOf(ten - count);
                    Toast.makeText(this, "1-"+ i, Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "对不起,不能降低等级", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.mine_askupdata:

                /**
                 *  如果已认证   count
                 *
                 *  未认证   money
                 */

                Integer integer = Integer.valueOf(gradeCode);
                Integer integer1 = Integer.valueOf(a);

                if (integer>integer1){
                    Toast.makeText(this, "对不起，不能降低等级", Toast.LENGTH_SHORT).show();
                }else if (integer==integer1){
                    Toast.makeText(this, "你已经是此等级", Toast.LENGTH_SHORT).show();
                }else {
                    //TODO 升级问题
                    int money = getMoney(a);
                    int money1 = getMoney(gradeCode);
                    int nowmoney = money - money1;

                    sendBook(nowmoney,a);
                }

                break;
        }
    }

    private int getMoney(String a) {
        mmm = 0;
        if (a.equals("0")){
            mmm = 0;
        }else if (a.equals("1")){
            mmm =0;
        }else if (a.equals("2")){
            mmm =100;
        }else if (a.equals("3")){
            mmm = 200;
        }else if (a.equals("4")){
            mmm = 500;
        }else if (a.equals("5")){
            mmm = 1000;
        }else if (a.equals("6")){
            mmm = 3000;
        }else if (a.equals("7")){
            mmm = 5000;
        }else if (a.equals("8")){
            mmm = 10000;
        }else if (a.equals("9")){
            mmm = 30000;
        }else if (a.equals("10")){
            mmm = 50000;
        }
        return mmm;
    }

    private void sendBook(int money,String a) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url+"/alipay/orderDeposit");
        params.setHeader("Content-Type","application/json");
        params.setHeader("token", token);
        params.setHeader("appId", appId);
        params.setHeader("userId", userId);
        final JSONObject json = new JSONObject();
        json.put("payType","03");
        json.put("total_amount",money+"");
        json.put("gradeCode",a);
        params.setBodyContent(json.toJSONString());
        Log.d("tag", "sendBook: 一喂员参数--"+money+"//"+a);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(final String result) {
                Log.d("tag", "onSuccess: 支付--"+result);
                JSONObject obj = JSON.parseObject(result);
                data = obj.getString("data");
                Log.d("tag", "onSuccess: 支付--"+data);
                if (data!=null){
                    final Runnable runnable = new Runnable() {
                        @Override
                        public void run() {
                            PayTask payTask = new PayTask(MineAskNumberActivty.this);
                            Map<String, String> map = payTask.payV2(data, true);
                            Message msg = new Message();
                            msg.what = SDK_PAY_FLAG;
                            msg.obj = map;
                            mHandler.sendMessage(msg);
                        }
                    };
                    Thread payThread = new Thread(runnable);
                    payThread.start();
                }else {

                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("tag", "onError: 一喂员--"+ex);
                ErrType.errType(MineAskNumberActivty.this,ex);

            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
            }
        });
    }
}
